package middlewares

import "net/http"

//自定义中间件
type Chain struct {
	middlewares []func(handler http.Handler) http.Handler
}


func Pipeline(next http.Handler) http.Handler {
	return AddMiddlewares(Middleware1,Middleware2,Middleware3).Then(next)

}


func AddMiddlewares(m ...func(handlerFunc http.Handler) http.Handler) Chain {

	c := Chain{}

	c.middlewares = append(c.middlewares,m...)

	return c

}


func (c Chain) Then(next http.Handler) http.Handler {

	for i := range c.middlewares {

		prev := c.middlewares[len(c.middlewares)-1-i]

		next = prev(next)
	}

	return next
}
